package com.winphone.uvam.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.winphone.uvam.common.BaseController;
import com.winphone.uvam.entity.Role;
import com.winphone.uvam.service.IRoleService;
import com.winphone.uvam.utils.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lwl
 * @since 2017-12-13
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    IRoleService iRoleService;

    /**
     * 分页查询
     * @param role
     * @param current
     * @param size
     * @return
     */
    @RequestMapping(value = "/allParam",method = RequestMethod.POST)
    @ResponseBody
    public Object allParam(Role role,
                           @RequestParam(value = "current",required = true) Integer current,
                           @RequestParam(value = "size",required = true) Integer size){
        Map<String, Object> condition = new HashMap<>();

        if (StringUtils.isNotEmpty(role.getRoleName())){
            condition.put("roleName",role.getRoleName());
        }

        if (StringUtils.isNotEmpty(role.getRoleCode())){
            condition.put("roleCode",role.getRoleCode());
        }
        Page<Role> pageInfo = iRoleService.selectPageParam(new Page<Role>(current,size),condition);
        return pageInfo;

    }

    /**
     * 获取所有 不分页
     * @return
     */
    @RequestMapping(value = "/getRoleList",method = RequestMethod.POST)
    @ResponseBody
    public Object getRoleList(Role role){
        List<Role> roles = iRoleService.selectParam(ObjectUtils.beanToMap(role));
        return renderSuccess(roles);
    }
}
